<?php

namespace App\Http\Controllers;

use App\Events\LoggedIn;
use App\Http\Controllers\Controller;
use App\Libs\GoogleAuth;
use App\Models\System;
use App\Models\User;
use Illuminate\Http\Request;
use App\Libs\SmsCode;

class AuthController extends Controller
{
  public function __construct()
  {
    $this->middleware('auth:api', ['except' => ['login', 'register']]);
  }

  public function login($role, Request $request)
  {
    if ($role === 'member') {
      $data = $this->memberLogin($request->mobile, $request->code);
    }

    if ($role === 'seller') {
      $data = $this->sellerLogin();
    }

    if ($role === 'admin') {
      $data = $this->adminLogin();
    }

    if ($role === 'agent') {
      $data = $this->agentLogin();
    }

    $user = auth()->user();

    $user->checkRoleEnable($role);
    $user->updateLogin();

    $user->singleLogin($data['token']);


    return success($data);
  }

  public function loginByPassword()
  {
    $credentials = request(['username', 'password']);
    if (!$token = auth()->attempt($credentials)) {
      fail('账号/密码错误');
    }
    return $token;
  }

  public function sellerLogin()
  {
    $token = $this->loginByPassword();

    $user = auth()->user();
    $user->checkWhitlist();
    $user->checkGa();

    return [
      'token' => $token,
      'token_type' => 'bearer',
      'user' => $user->loginInfo(),
      'sys' => System::map()
    ];
  }

  public function adminLogin()
  {
    $token = $this->loginByPassword();
    $user = auth()->user();

    return [
      'token' => $token,
      'token_type' => 'bearer',
      'user' => $user->loginInfo(),
      'taking_count' => takingCount(),
      'checking_count' => sellingCount(),
      'sys' => System::map(),
    ];
  }

  public function agentLogin()
  {
    $token = $this->loginByPassword();
    $user = auth()->user();

    return [
      'token' => $token,
      'token_type' => 'bearer',
      'user' => $user->loginInfo(),
      'sys' => System::map(),
    ];
  }

  public function me()
  {
    return success(auth()->user()->loginInfo());
  }

  public function logout()
  {
    auth()->logout();
    return success('ok');
  }

  public function refresh()
  {
    // return $this->respondWithToken(auth()->refresh());
  }

  public function register(Request $request)
  {
    // return success('ok');
  }

  public function info()
  {
    $user = auth()->user();

    $sys = System::map();
    if ($user->role === 'member') {
      $sys = $sys->only('sell_min_coins');
    }

    return success([
      'user' => $user->loginInfo(),
      'sys' => $sys
    ]);
  }

  public function memberLogin($mobile, $smscode)
  {
    SmsCode::check($mobile, $smscode);

    $user = User::byMobile($mobile);
    $token = auth()->login($user);

    return [
      'token' => $token,
      'token_type' => 'bearer',
      'user' => $user->loginInfo(),
      'sys' => System::map()->only('sell_min_coins')
    ];
  }

  public function passwordCheck(Request $request)
  {
    if (password_verify($request->password, auth()->user()->password)) {
      return success('ok');
    }
    fail('密码错误');
  }
}
